﻿Public Class ToolbarHeader
    Private FormTarget As Form
    Private FormExit As Form
    Private frmCtrl As New FormControl
    Private dgvBase As DataGridView
    Private dgvKey As String
    Public copyText As String
    Public DisableCMS As Boolean
    Public DefaultRow As Integer

    Public Sub Iniliaze(Title As String, CurrentForm As Form, LastForm As Form, DataGrid As DataGridView, DataGridKey As String)
        lblTitle.Text = Title
        FormTarget = CurrentForm
        CurrentForm.Text = Title
        FormExit = LastForm
        dgvBase = DataGrid
        dgvKey = DataGridKey
        If (dgvBase Is Nothing) Then
            ActionCtrl(True, False, False, False, False, False, False, True, True)
        Else
            If (DisableCMS = False) Then
                dgvBase.ContextMenuStrip = cmsBase
            End If
        End If
    End Sub

    Public Sub ActionCtrl(N As Boolean, V As Boolean, E As Boolean, D As Boolean, P As Boolean, S As Boolean, C As Boolean, R As Boolean, X As Boolean)
        tsNew.Visible = N
        tsView.Visible = V
        tsEdit.Visible = E
        tsDelete.Visible = D
        tsPrint.Visible = P
        tsSave.Visible = S
        tsCancel.Visible = C
        tsRefresh.Visible = R
        tsExit.Visible = X
        If (dgvBase IsNot Nothing) Then
            NewToolStripMenuItem.Enabled = N
            ViewToolStripMenuItem.Enabled = V
            EditToolStripMenuItem.Enabled = E
            DeleteToolStripMenuItem.Enabled = D
            RefreshToolStripMenuItem.Enabled = R
            ExitToolStripMenuItem.Enabled = X
        End If
    End Sub

    Public Event tsNewClick(ByVal sender As ToolbarHeader)
    Public Event tsEditClick(ByVal sender As ToolbarHeader)
    Public Event tsDeleteClick(ByVal sender As ToolbarHeader)
    Public Event tsPrintClick(ByVal sender As ToolbarHeader)
    Public Event tsSaveClick(ByVal sender As ToolbarHeader)
    Public Event tsCancelClick(ByVal sender As ToolbarHeader)
    Public Event tsRefreshClick(ByVal sender As ToolbarHeader)
    Public Event tsApproveClick(ByVal sender As ToolbarHeader)
    Public Event tsViewClick(ByVal sender As ToolbarHeader)

    Private Sub tsNew_Click(sender As Object, e As EventArgs) Handles tsNew.Click
        If (dgvBase IsNot Nothing) Then
            ActionCtrl(False, False, False, False, False, True, True, False, True)
            RaiseEvent tsNewClick(Me)
        End If
    End Sub

    Private Sub tsEdit_Click(sender As Object, e As EventArgs) Handles tsEdit.Click
        If (dgvBase IsNot Nothing) Then
            ActionCtrl(False, False, False, False, False, True, True, False, True)
            RaiseEvent tsEditClick(Me)
        End If
    End Sub

    Private Sub tsDelete_Click(sender As Object, e As EventArgs) Handles tsDelete.Click
        If (dgvBase IsNot Nothing) Then
            Dim rowI As Integer
            If (DefaultRow = 1) Then
                rowI = 0
            Else : rowI = dgvBase.CurrentRow.Index
            End If
            Dim answer As DialogResult
            answer = MessageBox.Show("Are you sure deleting data. " + dgvBase.Item(dgvKey, rowI).Value.ToString().Trim() + " ?", "Delete Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If answer = vbYes Then
                RaiseEvent tsDeleteClick(Me)
            End If
        End If
    End Sub

    Private Sub tsPrint_Click(sender As Object, e As EventArgs) Handles tsPrint.Click
        If (dgvBase IsNot Nothing) Then
            RaiseEvent tsPrintClick(Me)
        End If
    End Sub

    Private Sub tsSave_Click(sender As Object, e As EventArgs) Handles tsSave.Click
        If (dgvBase IsNot Nothing) Then
            Dim answer As DialogResult
            answer = MessageBox.Show("are you sure?", "Save Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If answer = vbYes Then
                RaiseEvent tsSaveClick(Me)
            End If
        End If
    End Sub

    Private Sub tsCancel_Click(sender As Object, e As EventArgs) Handles tsCancel.Click
        If (dgvBase IsNot Nothing) Then
            Dim answer As DialogResult
            answer = MessageBox.Show("All Change will be not saved, are you sure?", "Cancel Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If answer = vbYes Then
                RaiseEvent tsCancelClick(Me)
            End If
        End If
    End Sub

    Private Sub tsExit_Click(sender As Object, e As EventArgs) Handles tsExit.Click
        If (dgvBase IsNot Nothing) Then
            If (FormExit Is Nothing) Then
                FormTarget.Close()
            Else : frmCtrl.openChild(FormExit, FormTarget.ParentForm)
            End If
        End If
    End Sub

    Private Sub tsRefresh_Click(sender As Object, e As EventArgs) Handles tsRefresh.Click
        RaiseEvent tsRefreshClick(Me)
    End Sub

    Private Sub tsApprove_Click(sender As Object, e As EventArgs) Handles tsApprove.Click
        RaiseEvent tsApproveClick(Me)
    End Sub

    Private Sub tsView_Click(sender As Object, e As EventArgs) Handles tsView.Click
        RaiseEvent tsViewClick(Me)
    End Sub

    Private Sub ToolbarHeader_Load(sender As Object, e As EventArgs) Handles Me.Load
        If (dgvBase Is Nothing) Then
            ActionCtrl(True, False, False, False, False, False, False, True, True)
        End If
    End Sub

    Private Sub cmsBase_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles cmsBase.ItemClicked
        Select Case e.ClickedItem.Text
            Case "&New"
                    tsNew_Click(Nothing, Nothing)
            Case "&View"
                tsView_Click(Nothing, Nothing)
            Case "&Edit"
                    tsEdit_Click(Nothing, Nothing)
            Case "&Refresh"
                tsRefresh_Click(Nothing, Nothing)
            Case "&Delete"
                If (dgvBase IsNot Nothing) Then
                    Dim rowI As Integer = dgvBase.CurrentRow.Index
                    Dim answer As DialogResult
                    answer = MessageBox.Show("Are you sure deleting data. " + dgvBase.Item(dgvKey, rowI).Value.ToString().Trim() + " ?", "Delete Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    If answer = vbYes Then
                        tsDelete_Click(Nothing, Nothing)
                    End If
                End If
            Case "&Close"
                tsExit_Click(Nothing, Nothing)
            Case "Copy"
                Clipboard.SetText(copyText)
        End Select
    End Sub
End Class
